package com.vr.xxaiagent.tools;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.ai.model.function.FunctionCallback;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.ToolCallbacks;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 工具集中注册,本质上就是放到一个ToolCallback数组里
 *
 * @author hzh
 * @date 2025/06/05
 */
@Slf4j
@Configuration
public class ToolRegistration {
    @Value("${search-api.api-key}")
    private String searchApiKey;
    @Resource
    private ToolCallbackProvider toolCallbackProvider;
    @Bean
    public ToolCallback[] allTools() {
        FileOperationTool fileOperationTool = new FileOperationTool();
        WebSearchTool webSearchTool = new WebSearchTool(searchApiKey);
        WebScrapingTool webScrapingTool = new WebScrapingTool();
        ResourceDownloadTool resourceDownloadTool = new ResourceDownloadTool();
        TerminalOperationTool terminalOperationTool = new TerminalOperationTool();
        PDFGenerationTool pdfGenerationTool = new PDFGenerationTool();
        TerminateTool terminateTool = new TerminateTool();
        ToolCallback[] callbacks = ToolCallbacks.from(
                fileOperationTool,
                webSearchTool,
                webScrapingTool,
                resourceDownloadTool,
                terminalOperationTool,
                pdfGenerationTool,
                terminateTool
        );
        // 将mcp的工具添加进来
        if (toolCallbackProvider!=null&&toolCallbackProvider.getToolCallbacks().length > 0) {
            ToolCallback[] mcpToolCallbacks = (ToolCallback[]) toolCallbackProvider.getToolCallbacks();
            callbacks = ArrayUtils.addAll(callbacks, mcpToolCallbacks);
        }
        return callbacks;
    }
}
